class Student:
    def __init__(self, student_id, name):
        self.student_id = student_id
        self.name = name
        self.borrowed_books = []
    
    def borrow_book(self, book_id, manager):
        """请求借书"""
        success = manager.lend_book(self.student_id, book_id)
        if success:
            self.borrowed_books.append(book_id)
            print(f"学生 {self.name} 成功借阅书籍 {book_id}")
        else:
            print(f"学生 {self.name} 借阅书籍 {book_id} 失败")
        return success
    
    def return_book(self, book_id, manager):
        """请求还书"""
        if book_id in self.borrowed_books:
            success = manager.receive_book(self.student_id, book_id)
            if success:
                self.borrowed_books.remove(book_id)
                print(f"学生 {self.name} 成功归还书籍 {book_id}")
            else:
                print(f"学生 {self.name} 归还书籍 {book_id} 失败")
            return success
        else:
            print(f"学生 {self.name} 并未借阅书籍 {book_id}")
            return False
    
    def list_borrowed_books(self):
        """列出已借阅的书籍"""
        print(f"学生 {self.name} 借阅的书籍: {self.borrowed_books}")
        return self.borrowed_books
    
    def __str__(self):
        """返回学生信息"""
        return f"学生ID: {self.student_id}, 姓名: {self.name}"


class Book:
    def __init__(self, book_id, title, author, isbn, publication_date):
        self.book_id = book_id
        self.title = title
        self.author = author
        self.isbn = isbn
        self.publication_date = publication_date
        self.is_available = True  # 默认书籍可借
    
    def set_available(self, status):
        """设置书籍可用状态"""
        self.is_available = status
    
    def get_availability(self):
        """获取书籍可用状态"""
        return self.is_available
    
    def __str__(self):
        """返回书籍信息"""
        status = "可借" if self.is_available else "已借出"
        return f"书籍ID: {self.book_id}, 书名: {self.title}, 作者: {self.author}, 状态: {status}"


class LibraryManager:
    def __init__(self, manager_id, name):
        self.manager_id = manager_id
        self.name = name
        self.books = {}  # 存储书籍对象，key为book_id
        self.borrow_records = {}  # 存储借阅记录，key为student_id，value为book_id列表
    
    def lend_book(self, student_id, book_id):
        """处理借书操作"""
        if book_id not in self.books:
            print(f"书籍 {book_id} 不存在")
            return False
        
        book = self.books[book_id]
        
        if not book.get_availability():
            print(f"书籍 {book_id} 已被借出")
            return False
        
        # 更新书籍状态
        book.set_available(False)
        
        # 更新借阅记录
        if student_id not in self.borrow_records:
            self.borrow_records[student_id] = []
        self.borrow_records[student_id].append(book_id)
        
        print(f"管理员 {self.name} 处理了学生 {student_id} 借阅书籍 {book_id} 的请求")
        return True
    
    def receive_book(self, student_id, book_id):
        """处理还书操作"""
        if student_id not in self.borrow_records or book_id not in self.borrow_records[student_id]:
            print(f"学生 {student_id} 没有借阅书籍 {book_id} 的记录")
            return False
        
        if book_id not in self.books:
            print(f"书籍 {book_id} 不存在")
            return False
        
        book = self.books[book_id]
        
        # 更新书籍状态
        book.set_available(True)
        
        # 更新借阅记录
        self.borrow_records[student_id].remove(book_id)
        if not self.borrow_records[student_id]:  # 如果学生没有借阅其他书籍，删除记录
            del self.borrow_records[student_id]
        
        print(f"管理员 {self.name} 处理了学生 {student_id} 归还书籍 {book_id} 的请求")
        return True
    
    def add_book(self, book):
        """添加新书籍"""
        self.books[book.book_id] = book
        print(f"管理员 {self.name} 添加了书籍: {book.title}")
    
    def remove_book(self, book_id):
        """移除书籍"""
        if book_id in self.books:
            book_title = self.books[book_id].title
            del self.books[book_id]
            print(f"管理员 {self.name} 移除了书籍: {book_title}")
            return True
        else:
            print(f"书籍 {book_id} 不存在")
            return False
    
    def search_book(self, query):
        """搜索书籍"""
        results = []
        for book in self.books.values():
            if (query.lower() in book.title.lower() or 
                query.lower() in book.author.lower() or 
                query == book.isbn):
                results.append(book)
        
        print(f"搜索 '{query}' 找到 {len(results)} 本书籍")
        return results
    
    def list_all_books(self):
        """列出所有书籍（用于测试）"""
        print("图书馆所有书籍:")
        for book in self.books.values():
            print(f"  {book}")
        return list(self.books.values())
    
    def __str__(self):
        """返回管理员信息"""
        return f"管理员ID: {self.manager_id}, 姓名: {self.name}"
